package com.liu.yubi.config;

import org.jetbrains.annotations.NotNull;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * @author lqh
 * @since 2025-06-02 22:54
 * 自定义线程池配置
 */
@Configuration
public class ThreadPoolExecutorConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {
        //线程工厂（自定义线程名字等属性）
        ThreadFactory threadFactory=new ThreadFactory (){
            private int count=1;
            @Override
            public Thread newThread(@NotNull Runnable r) {
                //r：线程代执行的任务
                Thread thread = new Thread (r);
                //自定义线程名字
                thread.setName ("线程"+count);
                count++;
                return thread;
            }
        };
        //拒绝策略
        RejectedExecutionHandler rejectedExecutionHandler = (r, executor) -> {
        };

        //自定义线程池(正式员工：2 最多员工：4，临时工开除时间100s，任务队列大小：4)
        return new ThreadPoolExecutor (2,4,100, TimeUnit.SECONDS,
                new ArrayBlockingQueue<> (4),threadFactory,rejectedExecutionHandler);

    }
}
